package com.sctvcloud.bazhong.ui.activities;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.gson.Gson;
import com.jiongbull.jlog.JLog;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.base.BaseDetailActivity;
import com.sctvcloud.bazhong.beans.JSBeans;
import com.sctvcloud.bazhong.beans.MallJsonVo;
import com.sctvcloud.bazhong.ui.utils.TitleUtils;
import com.sctvcloud.bazhong.utils.UserManager;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ServiceH5Activity extends BaseDetailActivity {

    @BindView(R.id.webView_h5)
    protected WebView webView;

    private String url;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this).useAndSetTxTitle("服务");
        url = getIntent().getStringExtra(EX_URL);
        JLog.e("ServiceH5Activity", "---url=" + url);
        initWebView();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_service_h5);
        ButterKnife.bind(this);
    }

    private void initWebView() {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
        }
        webView.setWebChromeClient(new WebChromeClient() {
            // For Android  > 4.1.1
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
//                Log.i("test", "openFileChooser 3");
//                IntegralStoreActivity.this.uploadFile = uploadFile;
//                openFileChooseProcess(null);
            }

        });
        webView.setWebViewClient(new WebViewClient() {
            /**
             * 防止加载网页时调起系统浏览器
             */
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!url.startsWith("http://") && !url.startsWith("https://")) {
                    return true;
                }
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                String titleStr = view.getTitle();
                if (!TextUtils.isEmpty(titleStr)) {
//                    if (TextUtils.isEmpty(title)){
//                    title = titleStr;
//                    }
                }
            }
        });
        webView.addJavascriptInterface(new Object() {
            @JavascriptInterface
            public void callRouter(String s1, String s2) {
                //H5页面调用Android原生代码
                if (!TextUtils.isEmpty(s1)) {
                    try {
                        JLog.e("htmlLogin", "-----------------------s1-" + s1 + "----s2" + s2);
                        JSONObject jso = new JSONObject(s1);
                        MallJsonVo jsonBean = new Gson().fromJson(s1, MallJsonVo.class);
//                        parseJSMethod(jsonBean);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        }, "bridge");
        WebSettings settings = webView.getSettings();
        // 使用缓存
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        String ua = settings.getUserAgentString();
        settings.setUserAgentString(ua + " SCGC_UA");
//        settings.getUserAgentString()
        //使h5页面适应手机屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        //设置可以支持缩放
        settings.setSupportZoom(true);
        //设置出现缩放工具
        settings.setBuiltInZoomControls(true);
        //隐藏缩放工具
        settings.setDisplayZoomControls(false);
        //扩大比例的缩放
        settings.setUseWideViewPort(true);
        // 支持JavaScript
        settings.setJavaScriptEnabled(true);
//        settings.setMediaPlaybackRequiresUserGesture(false);

        if (!TextUtils.isEmpty(url)) {
            webView.loadUrl(url);
        }
    }
}
